import { reqGetCategory1List, reqGetCategory2List, reqGetCategory3List } from '@/api/product/category'
const state = {
  category1List: [], // 一级分类数据
  category2List: [], // 二级分类数据
  category3List: [], // 三级分类数据
  category1Id: '', // 一级分类ID
  category2Id: '', // 二级分类ID
  category3Id: '' // 三级分类Id
}

const actions = {
  // 发送请求获取一级分类数据
  async GetCategory1List({ commit }) {
    const res = await reqGetCategory1List()
    if (res.code === 200) {
      commit('SAVE_CATEGORY1_LIST', res.data)
    }
  },
  // 发送请求获取二级分类数据
  async GetCategory2List({ commit }, category1Id) {
    const res = await reqGetCategory2List(category1Id)
    if (res.code === 200) {
      commit('SAVE_CATEGORY2_LIST', { category2List: res.data, category1Id })
    }
  },
  // 发送请求获取三级分类数据
  async GetCategory3List({ commit }, category2Id) {
    const res = await reqGetCategory3List(category2Id)
    if (res.code === 200) {
      commit('SAVE_CATEGORY3_LIST', { category3List: res.data, category2Id })
    }
  }
}

const mutations = {
  // 存储一级分类数据到tate
  SAVE_CATEGORY1_LIST(state, category1List) {
    state.category1List = category1List
  },
  // 存储二级分类数据到tate
  SAVE_CATEGORY2_LIST(state, { category2List, category1Id }) {
    state.category2List = category2List
    // 清空category2Id和清空category3Id和
    state.category2Id = ''
    state.category3Id = ''
    // 更新category1Id
    state.category1Id = category1Id
  },
  // 存储三级分类数据到tate
  SAVE_CATEGORY3_LIST(state, { category3List, category2Id }) {
    state.category3List = category3List
    // 清空category3Id
    state.category3Id = ''
    // 更新category2Id
    state.category2Id = category2Id
  },
  // 存储category3Id
  SAVE_CATEGORY3_ID(state, category3Id) {
    // 更新category3Id
    state.category3Id = category3Id
  },
  // 重置数据
  RESET(state) {
    state.category1Id = ''
    state.category2List = []
    state.category3List = []
    state.category2Id = ''
    state.category3Id = ''
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
